package cn.ztelovecly.springboot.framework.util;

import cn.ztelovecly.springboot.framework.message.BaseMessage;
import com.alibaba.fastjson.JSON;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

public class MQUtil {
    private static Logger logger = LoggerFactory.getLogger(MQUtil.class);

    private static Boolean isProduction = false;
    private static String mqType = "";

    private static Producer onsProducer;
    private static DefaultMQProducer mqProducer;

    //@PostConstruct
    static
    {
        String deployMode = SettingUtil.getProperty("", "deploy.mode").toLowerCase();
        mqType = SettingUtil.getProperty("","mq.type");
        if(mqType!=null && !mqType.equals("")){
            switch (mqType.toLowerCase()){
                case "ons" :
                    Properties properties = new Properties();
                    properties.put(PropertyKeyConst.ProducerId, SettingUtil.getProperty("", "mq.producer.id"));
                    properties.put(PropertyKeyConst.AccessKey, SettingUtil.getProperty("", "aliyun.accesskey.id"));
                    properties.put(PropertyKeyConst.SecretKey, SettingUtil.getProperty("", "aliyun.accesskey.secret"));
                    onsProducer = ONSFactory.createProducer(properties);
                    onsProducer.start();
                    break;
                case "rocketmq" :
                    //发送邮件
                    mqProducer = new DefaultMQProducer(SettingUtil.getProperty("", "mq.producer.id"));
                    mqProducer.setNamesrvAddr(SettingUtil.getProperty("", "local.mq.address")); //本地开发环境才需要
                    try {
                        mqProducer.start();
                    } catch (MQClientException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
        // 兼容老版的配置启动方式
        else
        {
            if (!"production".equals(deployMode)) {
                isProduction = false;
                //发送邮件
                mqProducer = new DefaultMQProducer(SettingUtil.getProperty("", "mq.producer.id"));
                mqProducer.setNamesrvAddr(SettingUtil.getProperty("", "local.mq.address")); //本地开发环境才需要
                try {
                    mqProducer.start();
                } catch (MQClientException e) {
                    e.printStackTrace();
                }

            } else {
                isProduction = true;
                Properties properties = new Properties();
                properties.put(PropertyKeyConst.ProducerId, SettingUtil.getProperty("", "mq.producer.id"));
                properties.put(PropertyKeyConst.AccessKey, SettingUtil.getProperty("", "aliyun.accesskey.id"));
                properties.put(PropertyKeyConst.SecretKey, SettingUtil.getProperty("", "aliyun.accesskey.secret"));
                onsProducer = ONSFactory.createProducer(properties);
                onsProducer.start();
            }
        }
    }

    public static Boolean send(BaseMessage message){
        String messageBody = JSON.toJSONString(message);
        try {
            if(mqType!=null && !mqType.equals("")){
                switch (mqType.toLowerCase()){
                    case "ons" :
                        com.aliyun.openservices.ons.api.Message onsMsg = new com.aliyun.openservices.ons.api.Message(
                                message.getMsg_topic(),
                                message.getMsg_tag(),
                                messageBody.getBytes());
                        onsMsg.setKey(message.getMsg_key());
                        com.aliyun.openservices.ons.api.SendResult onsResult = onsProducer.send(onsMsg);

                        System.out.println(onsResult);
                        break;
                    case "rocketmq" :
                        Message rocketMsg = new Message(
                                message.getMsg_topic(),
                                message.getMsg_tag(),
                                message.getMsg_key(),
                                messageBody.getBytes());
                        SendResult rocketResult = mqProducer.send(rocketMsg);
                        System.out.println("id:" + rocketResult.getMsgId() +
                                " result:" + rocketResult.getSendStatus());
                        break;
                    default :
                        break;
                }
            }
            // 兼容旧的MQ模式
            else
            {
                if (!isProduction) {
                    Message msg = new Message(
                            message.getMsg_topic(),
                            message.getMsg_tag(),
                            message.getMsg_key(),
                            messageBody.getBytes());
                    SendResult result = mqProducer.send(msg);
                    System.out.println("id:" + result.getMsgId() +
                            " result:" + result.getSendStatus());
                } else {
                    com.aliyun.openservices.ons.api.Message msg = new com.aliyun.openservices.ons.api.Message(
                            message.getMsg_topic(),
                            message.getMsg_tag(),
                            messageBody.getBytes());
                    msg.setKey(message.getMsg_key());
                    com.aliyun.openservices.ons.api.SendResult result = onsProducer.send(msg);

                    System.out.println(result);
                }
            }
        }catch(Exception ex){
            logger.error(ex.getMessage(), ex);
//            ex.printStackTrace();
//            LogUtil.errorLog(ex);
            return false;
        }
        return true;
    }
}
